/**
 *
 * 关卡UI
 * @author 
 *
 */
class GuanKaUI extends egret.Sprite{
    /**剩余生命*/
    private lifeLeftTxt: egret.TextField;
    /**拥有金币*/
    private goldTxt: egret.TextField;
    /**当前时间*/
    private bos: egret.TextField = new egret.TextField;
    
    /**刷新时间*/
    private timeTxt: egret.TextField = new egret.TextField;
    private time: number = 120;
    /**下一波图标*/
    private alarm: egret.Bitmap;
    /**下一波图标*/
    private alarmType: egret.Bitmap;
    
    /**胜利位图*/
    private victoryBm: egret.Bitmap;
    /**失败位图*/
    private loseBm: egret.Bitmap;
    /**再试一次位图*/
    private tryAgainBm: egret.Bitmap;
    /**退出位图*/
    private quitBm: egret.Bitmap;
    /**黑色背景蒙板*/
    private mbBm: egret.Shape;
    
    private tool:UnitTool;
    
    private gold:number = 250;
    
    private topSp: egret.Sprite;
    private bottomSp: egret.Sprite;
    public rightSp: egret.Sprite;
    public battleSp: egret.Sprite;

    //信息面板
    public boardSp: egret.Sprite;
    public board: egret.Bitmap;
    public label: egret.TextField = new egret.TextField;
    public lifetxt: egret.TextField = new egret.TextField;
    public damagetxt: egret.TextField = new egret.TextField;
    public info: egret.TextField = new egret.TextField;
    public type: egret.TextField = new egret.TextField;
    public tip: egret.TextField = new egret.TextField;
    /**canClick*/
    private canClick: boolean = false;
    
    private bf: egret.BitmapFont;
    public battle:egret.BitmapText;
      
	public constructor(weaponLayer:egret.Sprite,objLayer:egret.Sprite) {
        super();
        var bm: egret.Bitmap;
        this.bf = RES.getRes("battle_fnt");
        
        
        //顶部容器
        this.topSp = new egret.Sprite();
        this.topSp.x = egret.MainContext.instance.stage.stageWidth - 140;
        this.topSp.y = -100;
        this.addChild(this.topSp);
        //左上角bg
        bm = Utils.createBitmapByName("uiinfo");
        bm.x = bm.y = 10;
        this.topSp.addChild(bm);
        
        //        //生命
        //        this.lifeLeftTxt = new egret.TextField;
        //        this.lifeLeftTxt.text = "0/0/0";
        //        this.lifeLeftTxt.size = 15;
        //        this.lifeLeftTxt.x = 75;
        //        this.lifeLeftTxt.y = 44;
        //        this.topSp.addChild(this.lifeLeftTxt);
        //金币
        this.goldTxt = new egret.TextField;

        this.goldTxt.text = "0";
        this.goldTxt.textColor = 0xffff00;
        this.goldTxt.fontFamily = "Helvetica";
        this.goldTxt.size = 16;
        this.goldTxt.x = 60;
        this.goldTxt.y = 15;
        this.topSp.addChild(this.goldTxt);
        this.goldTxt.anchorOffsetX = this.goldTxt.width / 2;
        
        //波数
        this.bos.text = "00:00";
        this.bos.fontFamily = "Helvetica";
        this.bos.size = 15
        this.bos.x = 75;
        this.bos.y = 44;
        this.topSp.addChild(this.bos);
        
        //战斗开始
        this.battleSp = new egret.Sprite();
        this.addChild(this.battleSp);
        this.battleSp.alpha = 1;
        
        
        this.battle = new egret.BitmapText();
        this.battle.font = this.bf;
        this.battle.letterSpacing = -1;
        this.battle.text = "第1战"
        this.battleSp.addChild(this.battle);
        this.battleSp.x = egret.MainContext.instance.stage.stageWidth / 2;
        this.battleSp.y = egret.MainContext.instance.stage.stageHeight / 3;
        this.battle.anchorOffsetX = this.battleSp.width / 2;        
        
        //信息面板容器
        this.boardSp = new egret.Sprite();
        this.addChild(this.boardSp);
        this.boardSp.visible = false;
        
        this.board = Utils.createBitmapByName("board");
        this.boardSp.addChild(this.board);
        
        this.label.text = "鹤雪";
        //this.label.bold = true;
        this.label.textColor= 0xffff00;
        this.label.fontFamily = "Microsoft YaHei";
        this.label.size = 18;
        this.label.x = 10;
        this.label.y = 8;
        this.boardSp.addChild(this.label);
        
        this.type.text = "防御"
        this.type.size = 15;
        this.type.x = 160;
        this.type.y = 13;
        this.boardSp.addChild(this.type);
        this.type.anchorOffsetX = this.type.width;
        
        this.info.text = "鹤雪好牛逼啊"
        this.info.width = 160;
        this.info.height = 50;
        this.info.size = 12;
        this.info.x = 10;
        this.info.y = 35;
        this.boardSp.addChild(this.info);
        
        this.tip.text = "点击切换该建筑所产生的兵种"
        this.tip.textColor = 0xffff00;
        this.tip.size = 10;
        this.tip.x = 10;
        this.tip.y = 70;
        this.boardSp.addChild(this.tip);
        
        this.lifetxt.text = "100";    
        this.lifetxt.size = 10;
        this.lifetxt.x = 35;
        this.lifetxt.y = 88;
        this.boardSp.addChild(this.lifetxt);
        
        this.damagetxt.text = "100";
        this.damagetxt.size = 10;
        this.damagetxt.x = 115;
        this.damagetxt.y = 88;
        this.boardSp.addChild(this.damagetxt);
        

        
        //刷兵提示
        this.rightSp = new egret.Sprite();
        this.rightSp.x = egret.MainContext.instance.stage.stageWidth-50;
        this.rightSp.y = 200;
        this.addChild(this.rightSp);
        
        this.alarm = Utils.createBitmapByName("attack_png");
        this.alarm.touchEnabled = true;
        this.alarm.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.attackTouch,this);
//        this.alarm.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.alarmTouch,this);
//        this.alarm.addEventListener(egret.TouchEvent.TOUCH_END,this.touchEnd,this);
        //this.alarm.x = GameSetting.swid;
        
        this.rightSp.addChild(this.alarm);
        
        //刷兵时间
        this.timeTxt.textColor = 0xff0000;

        this.timeTxt.fontFamily = "Helvetica";
        this.timeTxt.size = 13;
        this.timeTxt.text = "00:00";
        this.timeTxt.strokeColor = 0xffffb9;
        this.timeTxt.stroke = 1;
        this.timeTxt.x = 20;
        this.timeTxt.y = 40;
        this.rightSp.addChild(this.timeTxt);
        this.timeTxt.anchorOffsetX = this.timeTxt.width/2;
        
        this.rightSp.visible = false;
        
        
        
        //底部容器
        this.bottomSp = new egret.Sprite();
        this.bottomSp.x = 30;
        this.bottomSp.y = egret.MainContext.instance.stage.stageHeight;
        this.addChild(this.bottomSp);
        

        //返回地图按钮
        bm = Utils.createBitmapByName("pause");
        bm.x = 400;
        bm.y = 10;
        this.bottomSp.addChild(bm);
        bm.touchEnabled = true;
        bm.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.backToMapsHandle,this);
        
        //左下角bg
        bm = Utils.createBitmapByName("bar");
        bm.x = 0;
        bm.y = -5;
        this.bottomSp.addChild(bm);
        
        this.tool = new UnitTool(this.gold);
        this.tool.x= 5;
        this.tool.y = 3;
        this.bottomSp.addChild(this.tool);
        
	}
	/**返回按钮*/
	public backToMapsHandle(e:egret.TouchEvent){
        //GuankaBase.instance.pause();
        TweenMax.killAll();
        GuankaBase.instance.clear();
        this.dispatchEvent(new MainEvent(MainEvent.QUITGUANKA,null,true));
	}
	
	public onEnterFrame(advancedtime:number){
        this.tool.onEnterFrame(advancedtime);

	}
	
    /**UI出现动效*/
    public showUI() {
        TweenLite.to(this.topSp,0.3,{ y: 0,ease: Circ.easeOut });
        TweenLite.to(this.bottomSp,0.3,{ delay: 0.1,y: egret.MainContext.instance.stage.stageHeight-60,ease: Circ.easeOut });
        egret.Tween.get(this.battleSp).wait(1500).to({ alpha: 0 },1500).call(GuankaBase.instance.start,GuankaBase.instance);
    }
    
    public attackTouch(){
        GuankaBase.instance.attack();
        GuankaBase.instance.boing = true;
        GuankaBase.instance.atime =0;
        this.rightSp.visible =false;
    }
    public alarmTouch(){
        //this.enemyData[this.curBo]["type"];
        var guanka = GuankaBase.instance;
        var board = guanka.guankaUI.boardSp;
        board.alpha = 1;
        //GuankaBase.instance.removeChild(board);
        guanka.guankaUI.label.text = "第" + (GuankaBase.instance.curBo + 2)+"波";
        var unitType:number = guanka.enemyData[guanka.curBo + 1]["unitType"];
        var num: number = guanka.enemyData[guanka.curBo + 1]["count"];
        if(unitType==1){
            guanka.guankaUI.type.text="防御";
            guanka.guankaUI.info.text = "受到攻击型士兵攻击时减少30%伤害";
        } else if(unitType == 2){
            guanka.guankaUI.type.text = "攻击";
            guanka.guankaUI.info.text = "攻击秘术师时有一定几率造成双倍伤害";          
        }else{
            guanka.guankaUI.type.text = "秘术";
            guanka.guankaUI.info.text = "对防御型士兵额外造成30%伤害";
        }
        guanka.guankaUI.tip.text = "数量：" + num;
        guanka.guankaUI.lifetxt.text = guanka.enemyData[guanka.curBo+1]["life"];
        guanka.guankaUI.damagetxt.text = guanka.enemyData[guanka.curBo+1]["damage"];

        board.x = this.rightSp.x - 30;
        board.y = this.rightSp.y - 10;
    }
    protected touchEnd(){
        GuankaBase.instance.guankaUI.boardSp.visible=false;
    }
	/**设置金币数量*/
	public setGold(num:number){
        this.goldTxt.text = num.toString()+"(+"+GuankaBase.instance.goldUp+")";
        this.gold = num;
	}
    /**设置生命*/
    public setLife(){
        //this.lifeLeftTxt.text = GuankaBase.instance.lifeUp + "/" + GuankaBase.instance.damagekUp + "/" + GuankaBase.instance.goldUp;
    }
    /**设置波数*/
    public setBos(cur:number,total:number,atime:number){
        var min: string;
        var sec: string;
        if(cur > 9) {
            min = cur.toString();
        } else {
            min = "0" + cur.toString();
        }
        if(total > 9) {
            sec = total.toString();
        } else {
            sec = "0" + total.toString();
        }
        this.bos.text = min+":"+sec;
        
        if(atime > 0) {
            if(Math.floor(atime / 60) >0) {
                min = "0" + Math.floor(atime / 60).toString();
            } else{
                min = "00"
            }
            if(atime < 10) {
                sec = "0" + atime.toString();
            } else if(atime < 60){
                sec = atime.toString();
            } else if(atime%60<10) {
                sec = "0" + (atime%60).toString();
            } else{
                sec = (atime % 60).toString();
            } 
            this.timeTxt.text = min + ":" + sec;
        }else{
            this.rightSp.visible =false;
        }

    }
    
    
    /**胜利*/
    public showVictory(){
        //画一个黑色蒙板
        var sp: egret.Shape = new egret.Shape();
        sp.graphics.beginFill(0x000000,0.6);
        sp.graphics.drawRect(0,0,GameSetting.swid,GameSetting.shei);
        this.addChild(sp);
        sp.touchEnabled = true;
        sp.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.spTouch1,this);
        //胜利位图
        this.victoryBm = Utils.createBitmapByName("victory");
        this.victoryBm.anchorOffsetX = this.victoryBm.width / 2;
        this.victoryBm.anchorOffsetY = this.victoryBm.height / 2;
        this.victoryBm.x = GameSetting.swid / 2;
        this.victoryBm.y = 180;
        this.victoryBm.scaleX = this.victoryBm.scaleY = 0.1;
        this.addChild(this.victoryBm);
        
        
        TweenMax.to(this.victoryBm,0.5,{ scaleX: 1,scaleY: 1,ease: Back.easeOut });
        var that = this;
        TweenMax.delayedCall(15,function() {
            that.dispatchEvent(new MainEvent(MainEvent.QUITGUANKA,null,true));
        });
       //播放胜利音效
        SoundManager.playBgSound("victorysd",false);
    }
    
    /**失败*/
    public showLose(){
        //画一个黑色蒙板
        this.mbBm = new egret.Shape();
        this.mbBm.graphics.beginFill(0x000000,0.6);
        this.mbBm.graphics.drawRect(0,0,GameSetting.swid,GameSetting.shei);
        this.addChild(this.mbBm);
        this.mbBm.touchEnabled = true;
        this.mbBm.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.spTouch2,this);
        //失败位图
        this.loseBm = Utils.createBitmapByName("lose");
        this.loseBm.anchorOffsetX = this.loseBm.width / 2;
        this.loseBm.anchorOffsetY = this.loseBm.height / 2;
        this.loseBm.x = GameSetting.swid / 2;
        this.loseBm.y = -180;
        this.addChild(this.loseBm);
        //重新尝试按钮
        this.tryAgainBm = Utils.createBitmapByName("tryagain");
        this.tryAgainBm.anchorOffsetX = this.tryAgainBm.width / 2;
        this.tryAgainBm.anchorOffsetY = this.tryAgainBm.height / 2;
        this.tryAgainBm.x = GameSetting.swid / 2;
        this.tryAgainBm.y = 550;
        this.addChild(this.tryAgainBm);
        //退出按钮
        this.quitBm = Utils.createBitmapByName("quitgame");
        this.quitBm.anchorOffsetX = this.quitBm.width / 2;
        this.quitBm.anchorOffsetY = this.quitBm.height / 2;
        this.quitBm.x = GameSetting.swid / 2;
        this.quitBm.y = 550;
        this.addChild(this.quitBm);
        //lis
        this.tryAgainBm.touchEnabled = true;
        this.quitBm.touchEnabled = true;
        this.tryAgainBm.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.tryAgainHandle,this);
        this.quitBm.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.spTouch1,this);
        
        //UI动效
        TweenMax.to(this.loseBm,0.5,{ y: 150,ease: Cubic.easeOut });
        TweenMax.to(this.tryAgainBm,0.3,{ delay: 0.3,y: 240,ease: Cubic.easeOut });
        TweenMax.to(this.quitBm,0.3,{ delay: 0.4,y: 300,ease: Cubic.easeOut });
        
        //播放失败音效
    }
    
    
    /**返回地图界面*/
    private spTouch1(e:egret.TouchEvent){
        //TweenMax.killAll();
        SoundManager.stopBgSound();
        this.dispatchEvent(new MainEvent(MainEvent.QUITGUANKA,null,true));
        e.stopPropagation();
    }
    /**再次尝试*/
    private tryAgainHandle(e:egret.TouchEvent){
        //TweenMax.killAll();
        SoundManager.stopBgSound();
        this.dispatchEvent(new MainEvent(MainEvent.TRYAGAIN,null,true));
        this.removeChild(this.mbBm);
        this.removeChild(this.loseBm);
        this.removeChild(this.tryAgainBm);
        this.removeChild(this.quitBm);
        e.stopPropagation();
    }
    
    /**阻止事件*/
    private spTouch2(e:egret.TouchEvent){
        e.stopPropagation();
    }
	
}
